'use strict';

const Controller = require('egg').Controller;

class MenuController extends Controller {
    async create() {
        let { ctx, app } = this;
        ctx.validate(
            {
                menuname: {
                    type: "string",
                    required: true,
                    desc: "菜单名称",
                },
                menupath: {
                    type: "string",
                    required: true,
                    desc: "菜单路径",
                },
                icon: {
                    type: "string",
                    required: false,
                    desc: "图标",
                },
                fatherpath: {
                    type: "string",
                    required: true,
                    desc: "父路径",
                },
                sortnum: {
                    type: "string",
                    required: true,
                    desc: "排序",
                },
                haschildren: {
                    type: "string",
                    required: true,
                    desc: "是否有子节点",
                }
            }
        );
        let { menuname, menupath, icon, fatherpath, sortnum, haschildren } = ctx.request.body;
        if (
            await app.model.Menus.findOne({
                where: {
                    menuname,
                },
            }) || await app.model.Menus.findOne({
                where: {
                    menupath,
                },
            })
        ) {
            ctx.throw(400, "菜单已存在");
        }
        let res = await app.model.Menus.create({ menuname, menupath, icon, fatherpath, sortnum, haschildren });
        if (!res) {
            ctx.throw(400, "创建菜单失败");
        }
        return ctx.apiSuccess(res);
    }

    async allList() {
        let { ctx, app } = this;
        let res = await app.model.Menus.findAll({
            attributes: [
                "id",
                "menuname",
                "menupath",
                "icon",
                "fatherpath",
                "sortnum",
                "haschildren"
            ]
        })
        let bb = JSON.parse(JSON.stringify(res))
        bb.map(item => {
            item.label = item.menuname
        });
        // 调用递归方法
        let aa = this.generateOptions(bb)
        return ctx.apiSuccess(aa);
    }

    async getMenuOne() {
        let { ctx, app } = this;
        ctx.validate(
            {
                id: {
                    type: "string",
                    required: true,
                    desc: "菜单ID",
                }
            }
        );
        const id = ctx.query.id
        let res = await app.model.Menus.findOne({
            where: {
                id
            },
            attributes: [
                "id",
                "menuname",
                "menupath",
                "icon",
                "fatherpath",
                "sortnum",
                "haschildren"
            ]
        })
        return ctx.apiSuccess(res);
    }

    async updateMenuOne() {
        let { ctx, app } = this;
        ctx.validate({
            menuId: {
                type: "string",
                required: true,
                desc: "菜单Id"
            },
            menuname: {
                type: "string",
                required: true,
                desc: "菜单名称",
            },
            menupath: {
                type: "string",
                required: true,
                desc: "菜单路径",
            },
            icon: {
                type: "string",
                required: false,
                desc: "图标",
            },
            fatherpath: {
                type: "string",
                required: true,
                desc: "父路径",
            },
            sortnum: {
                type: "string",
                required: true,
                desc: "排序",
            },
            haschildren: {
                type: "string",
                required: true,
                desc: "是否有子节点",
            }
        });
        const { menuId, menuname, menupath, icon, fatherpath, sortnum } = ctx.request.body
        let menus = await app.model.Menus.findOne({
            where: {
                id: menuId
            },
            attributes: [
                "id",
                "menuname",
                "menupath",
                "icon",
                "fatherpath",
                "sortnum",
                "haschildren"
            ]
        })
        if (!menus) {
            ctx.throw(400, "获取菜单信息失败");
        }
        let res = await menus.update({ id: menuId, menuname, menupath, icon, fatherpath, sortnum })
        return ctx.apiSuccess(res);
    }

    async removeMenu() {
        let { ctx, app } = this;
        ctx.validate({
            menuId: {
                type: "string",
                required: true,
                desc: "菜单Id"
            }
        });
        const { menuId } = ctx.request.body
        let menus = await app.model.Menus.findOne({
            where: {
                id: menuId
            },
            attributes: [
                "id",
                "menuname",
                "menupath",
                "icon",
                "fatherpath",
                "sortnum",
                "haschildren"
            ]
        })
        if (!menus) {
            ctx.throw(400, "获取菜单信息失败");
        }
        await menus.destroy({
            where: {
                id: menuId,
            },
        });
        return ctx.apiSuccess("删除成功")
    }


    // 开始递归方法
    generateOptions(params) {
        const result = []
        for (const param of params) {
            if (param.fatherpath == 0) { // 判断是否为顶层节点
                param.children = this.getchilds(param.id, params) // 获取子节点
                result.push(param)
            }
        }
        return result
    }
    getchilds(id, array) {
        const childs = []
        for (const arr of array) { // 循环获取子节点
            if (arr.fatherpath == id) {
                childs.push(arr)
            }
        }
        for (const child of childs) { // 获取子节点的子节点
            const childscopy = this.getchilds(child.id, array) // 递归获取子节点
            if (childscopy.length > 0) {
                child.children = childscopy
            }
        }
        return childs
    }
}

module.exports = MenuController;
